package com.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.common.R;
import com.springboot.pojo.Category;
import com.springboot.pojo.Setmeal;
import com.springboot.pojo.SetmealDish;
import com.springboot.pojo.SetmealDto;
import com.springboot.service.CategoryService;
import com.springboot.service.SetmealDishService;
import com.springboot.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.pattern.PathPattern;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

   @PostMapping
   @CacheEvict(value = "setmealList" ,allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){
         setmealService.setmealWithDish(setmealDto);
       return R.success("新增套餐成功!");
   }

   @GetMapping("/page")
    public  R<Page> page(int page,int pageSize,String name) {
       //分页构造器
       Page<Setmeal> pageParam = new Page<>(page, pageSize);
       Page<SetmealDto>  page2=new Page<>();
       //条件构造器
       LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper();
       wrapper.like(name!=null,Setmeal::getName,name);
        //添加排序条件
        wrapper.orderByDesc(Setmeal::getUpdateTime);
       setmealService.page(pageParam,wrapper);
        //对象拷贝
       BeanUtils.copyProperties(pageParam,page2,"records");
       List<Setmeal> records = pageParam.getRecords();
       List<SetmealDto> setmealDtos =records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category  category = categoryService.getById(categoryId);
            if (category!=null){
                String name1 = category.getName();
                setmealDto.setCategoryName(name1);
            }
            return   setmealDto;
        }).collect(Collectors.toList());
         page2.setRecords(setmealDtos);
          return  R.success(page2);
   }

    /**
     * 根据id删除套餐
     * @param ids
     * @return
     */
   @DeleteMapping
   @CacheEvict(value = "setmealList" ,allEntries = true)
   //删除setmealList分类下的所以缓存数据
    public R<String> delete(@RequestParam List<Long> ids){
         setmealService.removeWithDish(ids);
       return  R.success("删除套餐成功!");
   }

    /**
     * 根据id更新套餐状态
     * @return
     */
   @PostMapping("/status/{status}")
    public R<String> setstatus(@PathVariable int status,@RequestParam List<Long> ids){
          for (long id : ids){
              Setmeal byId = setmealService.getById(id);
              Integer status1 = byId.getStatus();
              if (status1==1){
                  byId.setStatus(0);
              } else if (status1==0) {
                  byId.setStatus(1);
              }
                setmealService.updateById(byId);
          }
       return R.success("更新状态成功!");
   }

    /**
     * 根据条件查询数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "steamList",key = "#setmeal.categoryId+'_'+#setmeal.status")
   public  R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(Setmeal::getStatus, setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);

    }

   }

